package cn.yangtianxia.org.controller;

import cn.yangtianxia.basic.exception.BusinessException;
import cn.yangtianxia.basic.util.ExcelUtil;
import cn.yangtianxia.basic.vo.ShopVo;
import cn.yangtianxia.org.domain.ShopAuditLog;
import cn.yangtianxia.org.service.IShopService;
import cn.yangtianxia.org.domain.Shop;
import cn.yangtianxia.org.query.ShopQuery;
import cn.yangtianxia.basic.util.AjaxResult;
import cn.yangtianxia.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    @GetMapping("/echars")
    public Map<String, Object> shopEchars(){
        List<ShopVo> shopVos = shopService.loadCount();
        ArrayList<String> states = new ArrayList<>();
        ArrayList<Long> nums = new ArrayList<>();
        for (ShopVo shopVo : shopVos) {
            states.add(shopVo.getState());
            nums.add(shopVo.getNum());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x", states);
        map.put("y", nums);
        return map;
    }

    //excel导入
    @GetMapping("/export/excel")
    public void exportExcel( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.loadAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //excel导出
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart MultipartFile file){
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        shopService.batchAdd(list);
    }

   //店铺注册入库后进行人工审核
   //审核状态：通过（激活），驳回，拒绝
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());//自定义异常
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }


    /**
     * 驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());//自定义异常
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }




    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());//自定义异常
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
